package com.zhou.yygh.cmn.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhou.yygh.cmn.listener.DictListener;
import com.zhou.yygh.cmn.mapper.DictMapper;
import com.zhou.yygh.cmn.service.DictService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import zhou.hospital.model.cmn.Dict;
import zhou.hospital.vo.cmn.DictEeVo;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {


    //获取改用户所有子节点
    @Override
    public List<Dict> findChildDate(Long id) {
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", id);
        List<Dict> list = baseMapper.selectList(wrapper);
        if (list!=null){
            for (Dict dict : list) {
                dict.setHasChildren(isChild(dict.getId()));
            }
        }
        return list;

    }

   //是否有子节点
    private boolean isChild(long id){
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", id);
        Integer count = baseMapper.selectCount(wrapper);
        return count>0;
    }

    //将数据以excel文件导出
   @Override
    public  void  exportDictDate(HttpServletResponse response){
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = "dict";
        response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");
        List<Dict> list = baseMapper.selectList(null);
        List<DictEeVo> dictVo = new ArrayList<>();
        for (Dict dict : list) {
            DictEeVo dictEeVo = new DictEeVo();
            BeanUtils.copyProperties(dict,dictEeVo);
            dictVo.add(dictEeVo);
        }
        try {
            EasyExcel.write(response.getOutputStream(),DictEeVo.class).sheet("dict")
                    .doWrite(dictVo);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //导入数据字典
    public void importDictData(MultipartFile file){
        try {
            EasyExcel.read(file.getInputStream(),DictEeVo.class,new DictListener()).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    //查询数据字典名称
    @Override
    public String getDictName(String dictCode, String value) {
        if(StringUtils.isEmpty(dictCode)){
            QueryWrapper<Dict> wrapper = new QueryWrapper<>();
            wrapper.eq("value",value);
            Dict dict = baseMapper.selectOne(wrapper);
            return dict.getName();
        }else {
            //根据 dict_code查询 根数据
            QueryWrapper<Dict> wrapperDictCode = new QueryWrapper();
            wrapperDictCode.eq("dict_code",dictCode);
            Dict dictParent = baseMapper.selectOne(wrapperDictCode);
            Long parentId = dictParent.getId();

          //根据父节点和value 查询数据
            Dict dict = baseMapper.selectOne(new QueryWrapper<Dict>()
                    .eq("parent_id", parentId)
                    .eq("value", value));
            return dict.getName();
        }

    }


    //根据dictCode查询子节点
    @Override
    public List<Dict> findByDictCode(String dictCode) {
        //根据dictCode查询该节点信息，
        QueryWrapper<Dict> wrapperDictCode = new QueryWrapper();
        wrapperDictCode.eq("dict_code",dictCode);
        Dict dictParent = baseMapper.selectOne(wrapperDictCode);
        Long parentId = dictParent.getId();

        //查询该节点所有的子节点
        List<Dict> dictList = this.findChildDate(parentId);
        return dictList;


    }
}
